package com.csu.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.annotation.SaMode;
import com.csu.common.CommonResponse;
import com.csu.entity.Audit;
import com.csu.entity.Classes;
import com.csu.entity.Subject;
import com.csu.entity.User;
import com.csu.service.UserService;
import com.csu.vo.*;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class UserController {
    @Autowired
    UserService userService;

    /**
     * 登录功能
     */
    @PostMapping("/login")
    //@SaIgnore
    public CommonResponse<Object> login(@RequestBody LoginReqVO loginReqVO){
        System.out.println("登录！");
        return userService.login(loginReqVO.getAccountName(), loginReqVO.getPassword());
    }

    /**
     * 查找class_id为一定值的所有学生。
     */
    @GetMapping("/users/students/{classId}")
    //@SaIgnore
    public CommonResponse<List<UserVo>> getStudents(@PathVariable int classId) throws JsonProcessingException {
        return userService.getStudents(classId);
    }

    /**
     * 获取班级列表
     */
    @GetMapping("/classes")
    //@SaIgnore
    public CommonResponse<List<ClassesRespVO>> getClasses() throws JsonProcessingException {
        return userService.getClasses();
    }

    /**
     * role_id:学生（3），家长（2），老师（1），管理员（0）
     * 注册：家长，老师放审核表；学生，管理员放user表
     */
    @PostMapping("/register")
    //@SaIgnore
    public CommonResponse<Object> register(@RequestBody RegisterReqVO registerReqVO){
        return userService.register(registerReqVO.getClassId(), registerReqVO.getPassword(),registerReqVO.getRoleId(),
                registerReqVO.getAccountName(), registerReqVO.getStudentId(), registerReqVO.getSubjectId());
    }

    /**
     * 改用户名
     */
    @PutMapping("/users/username")
    //@SaCheckRole("user")
    public CommonResponse<Object> changeUsername(@RequestBody ChangeUsernameReqVO changeUsernameReqVO, @RequestParam int id){
        return userService.changeUsername(changeUsernameReqVO.getName(), id);
    }

    /**
     * 改密码
     */
    @PutMapping("/users/password")
    //@SaCheckRole("user")
    public CommonResponse<Object> changePassword(@RequestBody ChangePasswordReqVO changePasswordReqVO, @RequestParam int id){
        return userService.changePassword(changePasswordReqVO.getPassword(), id);
    }

    /**
     * 获取待审核的家长列表
     */
    @GetMapping("/audits/parents")
    //@SaCheckRole("teacher")
    public CommonResponse<List<AuditVo>> getAuditParents(@RequestParam int id){
        return userService.getAuditParents(id);
    }

    /**
     * 获取待审核的老师列表
     */
    @GetMapping("/audits/teachers")
    //@SaCheckRole("admin")
    public CommonResponse<List<AuditVo>> getAuditTeachers(){
        return userService.getAuditTeachers();
    }

    /**
     * 审核不通过，删除第audit_id条审核信息
     */
    @DeleteMapping("/audits/{auditId}")
    //@SaCheckRole(value = {"teacher", "admin"}, mode = SaMode.OR)
    public CommonResponse<Object> deleteAudit(@PathVariable int auditId){
        return userService.deleteAudit(auditId);
    }

    /**
     * 第id条信息审核通过，添加用户信息到user,PS等。
     */
    @PostMapping("/audits/{auditId}")
    //@SaCheckRole(value = {"teacher", "admin"}, mode = SaMode.OR)
    public CommonResponse<Object> passAudit(@PathVariable int auditId){
        return userService.passAudit(auditId);
    }

    /**
     * 获取学科列表
     */
    @GetMapping("/users/subjects/{classId}")
    //@SaIgnore
    public CommonResponse<List<Subject>> getSubjects(@PathVariable int classId){
        return userService.getSubjects(classId);
    }
    /**
     * 获取孩子名字
     */
    @GetMapping("/users/child/{userId}")
    //@SaCheckRole("parent")
    public CommonResponse<List<StudentRespVO>> getChildren(@PathVariable int userId){
        return userService.getChildren(userId);
    }
}
